/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.DbConfigSelf;

public class ServerThreadHttp extends Thread
{
	public ExecutorService	executorService;	// 线程
	// private final int POOL_SIZE = 5; // 单个CPU线程池
	public ServerSocket		accSocket;			// 接收数据socket
	private String			strInput	= "";
	private String			strNum		= "";

	public void setStrInput(String strInput)
	{
		this.strInput = strInput;
	}

	public String getStrNum()
	{
		return strNum;
	}

	public void setStrNum(String strNum)
	{
		this.strNum = strNum;
	}

	public ServerThreadHttp(String Input, String Num)
	{
		this.strNum = Num;
		this.strInput = Input;
	}

	public String getStrInput()
	{
		return strInput;
	}

	@SuppressWarnings("static-access")
	public void run()
	{
		try
		{
			this.setName("ServerThreadHttp_zxyong");
			accSocket = new ServerSocket(DbConfigSelf.instance.port);
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		// 创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程。
		executorService = Executors.newCachedThreadPool();
		while(true)
		{
			Socket socket = null;
			try
			{
				socket = accSocket.accept();
				// 超时机制
				// socket.setSoTimeout(30000);
				executorService.execute(new ServerHandlerHttp(socket));
			}
			catch(Exception e)
			{
				System.out.println("error executorService"+ Thread.activeCount() + e.getMessage());
			}
			finally
			{
				if(!java.util.Objects.equals(socket,null))
				{
					// socket.setSoLinger(true, 0);
					// socket.shutdownInput();
					// socket.shutdownOutput();
					// socket.close();
				}
			}
			try
			{
				Thread.sleep(10);
			}
			catch(Exception e)
			{
			}
		}
	}
}